#include "log/log.h"
#include "cJSON/cJSON.h"
#include <stdlib.h>

int main(int argc,const char *argv[])
{
    //生成json字符串
    cJSON *json1 = cJSON_CreateObject();
    cJSON_AddStringToObject(json1,"name","zhangsan");
    cJSON_AddNumberToObject(json1,"age",18);
    cJSON_AddStringToObject(json1,"address","beijing");
    char *json_str = cJSON_PrintUnformatted(json1);
    log_debug("json_str:%s\n",json_str);
    //解析json字符串
    cJSON *json2 = cJSON_Parse(json_str);
    if(json2 == NULL)
    {
        log_error("解析失败\n");
        return -1;
    }
    char *name = cJSON_GetObjectItem(json2,"name")->valuestring;
    int age = cJSON_GetObjectItem(json2,"age")->valueint;
    log_debug("name:%s,age:%d\n",name,age);
    
    //释放内存
    cJSON_Delete(json1);
    cJSON_Delete(json2);
    free(json_str);
    return 0;
}